package com.bqtv.sport.commen.utils

import android.text.TextUtils
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.tencent.mmkv.MMKV


class MMKVUtils {
    companion object {
        private const val CONFIG_NAME = "setting"
        const val COLLECT_MSG = "CollectMsg"

        /**
         * 获取MMKV
         *
         * @return
         */
        private fun getMMKV(): MMKV? {
            return MMKV.mmkvWithID(CONFIG_NAME)
        }

        fun getData(paramName: String?): String? {
            return getMMKV()!!.decodeString(paramName, "")
        }

        fun getDataBool(paramName: String?): Boolean? {
            return getMMKV()!!.decodeBool(paramName)
        }

        fun getDataInt(paramName: String?): Int? {
            return getMMKV()!!.decodeInt(paramName)
        }

        fun getDataFloat(paramName: String?): Float? {
            return getMMKV()!!.decodeFloat(paramName)
        }

        fun saveData(
            paramName: String?,
            paramValue: String?
        ) {
            if (!TextUtils.isEmpty(paramValue)) {
                getMMKV()!!.encode(paramName, paramValue)
            }
        }

        fun saveData(
            paramName: String?,
            paramValue: Boolean
        ) {
            getMMKV()!!.encode(paramName, paramValue)
        }

        fun saveData(
            paramName: String?,
            paramValue: Int
        ) {
            getMMKV()!!.encode(paramName, paramValue)
        }

        fun saveData(
            paramName: String?,
            paramValue: Float
        ) {
            getMMKV()!!.encode(paramName, paramValue)
        }

        fun saveStringList(
            paramName: String?,
            dataArr: List<String>
        ) {
            if (dataArr == null) {
                return
            }
            var gson = Gson()
            saveData(paramName, gson.toJson(dataArr));
        }

        fun saveStringList(
            paramName: String?,
            paramValue: String
        ) {
            var dataArr: ArrayList<String> = getStringList(paramName)
            if (dataArr == null) {
                dataArr = ArrayList<String>();
            }
            dataArr.add(paramValue);
            var gson = Gson()
            saveData(paramName, gson.toJson(dataArr));
        }

        fun getStringList(paramName: String?): ArrayList<String> {
            var dataArr: ArrayList<String> = arrayListOf()
            if (!TextUtils.isEmpty(getData(paramName))) {
                var gson: Gson = Gson()
                dataArr = gson.fromJson(
                    getData(paramName),
                    object : TypeToken<ArrayList<String?>?>() {}.type
                )
            }

            return dataArr
        }

        /**
         * 重置信息
         *
         * @return
         */
        fun resetUserInfo() {
            // 载入配置文件
            getMMKV()!!.clear()
        }

        fun resetUserInfo(paramName: String?) {
            // 载入配置文件
            getMMKV()!!.remove(paramName)
        }
    }
}